<template>
  <el-dialog
    title="分配角色"
    :visible.sync="visible"
    @close="handelLoad"
  >
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="handelLoad">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { userInById, assignRoles } from '@/api/user'

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 所有角色
      checkList: [], // 被选中
      id: '', // 用户的id
      roleIds: '' // 角色id
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取所有角色权限
    async  getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 100 })
      this.list = rows
    },
    // 关闭弹框
    handelLoad() {
      this.$emit('update:visible', false)
    },
    // 获取用户详情
    async getUserInfo(id) {
      this.id = id
      const { roleIds } = await userInById(id)
      console.log(roleIds)
      this.checkList = roleIds
    },
    // 更新用户角色
    async submit() {
      await assignRoles({ id: this.id, roleIds: this.checkList })
      this.$message.success('更新成功')
      this.handelLoad()
    }
  }
}
</script>
